package com.barbeque.mall.service.wx;

import com.barbeque.mall.bean.comment.MarketComment;
import com.barbeque.mall.bean.comment.MarketCommentExample;
import com.barbeque.mall.bean.wx.comment.*;
import com.barbeque.mall.mapper.MarketCommentMapper;
import com.barbeque.mall.mapper.WxMarketCommentMapper;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.barbeque.mall.bean.param.CommonData;
import com.barbeque.mall.bean.user.MarketUser;
import com.barbeque.mall.mapper.MarketCommentMapper;
import com.barbeque.mall.mapper.MarketUserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Date;
import java.util.List;

/**
 * @author 苏东宇
 * @since 2022/07/21 16:55
 */

/**
 * @author why
 * @since 2022/07/21 16:24
 */

@Service
public class WxCommentServiceImpl implements WxCommentService {

    @Autowired
    MarketCommentMapper marketCommentMapper;

    @Autowired
    MarketUserMapper marketUserMapper;

    @Autowired
    WxMarketCommentMapper wxMarketCommentMapper;

    /**
     * 根据商品id获取所有评论
     *
     * @param goodsId
     * @return java.util.List<com.barbeque.mall.bean.comment.MarketComment>
     * @author 苏东宇
     * @since 2022/07/21 16:57
     */
    @Override
    public List<MarketComment> getCommentByGoodsId(Integer goodsId) {
        MarketCommentExample marketCommentExample = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = marketCommentExample.createCriteria();
        criteria.andTypeEqualTo(new Byte("0"));
        criteria.andValueIdEqualTo(goodsId);
        criteria.andDeletedEqualTo(false);

        List<MarketComment> marketComments = marketCommentMapper.selectByExample(marketCommentExample);


        return marketComments;
    }

    @Override
    public CommonData<WxCommentList> selectTopicCommentList(Integer valueId,Integer showType, TopicCommentPageInfo info) {
        PageHelper.startPage(info.getPage(), info.getLimit());
//        if(showType == 1){
//
//            List<WxCommentList> wxComment = marketCommentMapper.selectByValueIDShowType(valueId,showType);
//            for (WxCommentList wxCommentList : wxComment) {
//                Integer userId = wxCommentList.getUserId();
//                CommentUserInfo userInfoDTO = marketUserMapper.selectByUser(userId);
//                wxCommentList.setUserInfo(userInfoDTO);
//            }
//            PageInfo<WxCommentList> wxCommentListPageInfo = new PageInfo<>(wxComment);
//            return CommonData.data(wxCommentListPageInfo);
//        }
        List<WxCommentList> wxComment = marketCommentMapper.selectByValueID(valueId);
        for (WxCommentList wxCommentList : wxComment) {
            Integer userId = wxCommentList.getUserId();
            CommentUserInfo userInfoDTO = marketUserMapper.selectByUser(userId);
            wxCommentList.setUserInfo(userInfoDTO);
        }
        PageInfo<WxCommentList> wxCommentListPageInfo = new PageInfo<>(wxComment);
        return CommonData.data(wxCommentListPageInfo);
    }

    @Override
    public WxMarketComment insertTopicComment(WxMarketComment wxMarketComment) {
        MarketUser primaryPrincipal = (MarketUser) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        Integer userId = primaryPrincipal.getId();

        wxMarketComment.setUserId(userId);

        Date date = new Date(System.currentTimeMillis());
        wxMarketComment.setAddTime(date);
        wxMarketComment.setUpdateTime(date);

        // 执行插入操作
        wxMarketCommentMapper.insertSelective(wxMarketComment);

        // 执行查询操作，作为返回数据
         wxMarketComment = wxMarketCommentMapper.selectByPrimaryKey(wxMarketComment.getId());

         return wxMarketComment;
    }

    @Override
    public CommentCount selectCommentListCount(Integer valueId) {
        Integer allCount = marketCommentMapper.selectCountAll(valueId);
        Integer hasPicCount = marketCommentMapper.selectCountHasPic(valueId);

        CommentCount commentCount = new CommentCount();
        commentCount.setAllCount(allCount);
        commentCount.setHasPicCount(hasPicCount);
        return commentCount;
    }
}
